<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>eval manual page - Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> eval</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
eval &mdash; Evaluate a Tcl script
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>eval </B><I>arg </I>?<I>arg ...</I>?<BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
<B>Eval</B> takes one or more arguments, which together comprise a Tcl
script containing one or more commands.
<B>Eval</B> concatenates all its arguments in the same
fashion as the <B><A HREF="../TclCmd/concat.htm">concat</A></B> command, passes the concatenated string to the
Tcl interpreter recursively, and returns the result of that
evaluation (or any error generated by it).
Note that the <B><A HREF="../TclCmd/list.htm">list</A></B> command quotes sequences of words in such a
way that they are not further expanded by the <B>eval</B> command.
<H3><A NAME="M5">EXAMPLES</A></H3>
Often, it is useful to store a fragment of a script in a variable and
execute it later on with extra values appended. This technique is used
in a number of places throughout the Tcl core (e.g. in <B><A HREF="../TclCmd/fcopy.htm">fcopy</A></B>,
<B><A HREF="../TclCmd/lsort.htm">lsort</A></B> and <B><A HREF="../TclCmd/trace.htm">trace</A></B> command callbacks). This example shows how
to do this using core Tcl commands:
<P>
<PRE>set script {
    puts &quot;logging now&quot;
    lappend $myCurrentLogVar
}
set myCurrentLogVar log1
# Set up a switch of logging variable part way through!
after 20000 set myCurrentLogVar log2

for {set i 0} {$i&lt;10} {incr i} {
    # Introduce a random delay
    after [expr {int(5000 * rand())}]
    update    ;# Check for the asynch log switch
    <B>eval</B> $script $i [clock clicks]
}</PRE>
<P>
Note that in the most common case (where the script fragment is
actually just a list of words forming a command prefix), it is better
to use <B>{*}$script</B> when doing this sort of invocation
pattern.  It is less general than the <B>eval</B> command, and hence
easier to make robust in practice.
The following procedure acts in a way that is analogous to the
<B><A HREF="../TclCmd/lappend.htm">lappend</A></B> command, except it inserts the argument values at the
start of the list in the variable:
<P>
<PRE>proc lprepend {varName args} {
    upvar 1 $varName var
    # Ensure that the variable exists and contains a list
    lappend var
    # Now we insert all the arguments in one go
    set var [<B>eval</B> [list linsert $var 0] $args]
}</PRE>
<P>
However, the last line would now normally be written without
<B>eval</B>, like this:
<P>
<PRE>set var [linsert $var 0 {*}$args]</PRE>
<H3><A NAME="M6">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/catch.htm">catch</A></B>, <B><A HREF="../TclCmd/concat.htm">concat</A></B>, <B><A HREF="../TclCmd/error.htm">error</A></B>, <B><A HREF="../TclCmd/tclvars.htm">errorCode</A></B>, <B><A HREF="../TclCmd/tclvars.htm">errorInfo</A></B>, <B><A HREF="../TclCmd/interp.htm">interp</A></B>, <B><A HREF="../TclCmd/list.htm">list</A></B>, <B><A HREF="../TclCmd/namespace.htm">namespace</A></B>, <B><A HREF="../TclCmd/subst.htm">subst</A></B>, <B><A HREF="../TclCmd/uplevel.htm">uplevel</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
<A href="../Keywords/C.htm#concatenate">concatenate</A>, <A href="../Keywords/E.htm#evaluate">evaluate</A>, <A href="../Keywords/S.htm#script">script</A>
<div class="copy">Copyright &copy; 1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1996 Sun Microsystems, Inc.
</div>
</BODY></HTML>
